﻿using Function;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UI.UserControl.Base;


namespace UI.Module.ShopManage
{
    public partial class ShopUserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string shopid = Request.QueryString["shipID"] ?? "";
                if (shopid != "")
                {
                    hidWhere.Value = "and a.ShopID=" + shopid;
                    LoadPage(shopid);
                }
                else {
                    LoadPage("");
                }
            }
        }
        private void LoadPage(string shopid)
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "ShopID,(select STitle from dbo.ST_Shop where id=ShopID)as STitle", "GX_InvestorShop", " InvestorID=" + Method.CurrentUserID, "[ID] desc", PubConstant.DataBaseTarget.JHShop, out Message);
            ddlistShop.DataSource = Page_Dt;
            ddlistShop.DataTextField = "STitle";
            ddlistShop.DataValueField = "ShopID";
            ddlistShop.DataBind();
            if (shopid != "")
            {
                ddlistShop.SelectedValue = shopid;
            }
            else
            {
                ddlistShop.Items.Insert(0, new ListItem() { Value = "0", Text = "--所有门店人员--" });
            }
            
        }

        string Message = "";
        private void Bind(string Where_String = "")
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "case when b.[status]=1 then '正常' when b.[status]=2 then '禁用' when b.[status]=3 then '关闭' end as statusNote,(select note from dy_shopusertype where code=a.shopusertype)as typeNote ,shopuserid,truename,mobilephone", "dbo.ST_ShopUser a left join Permission.[User] b on a.shopuserid=b.userid  left join GX_InvestorShop c on a.shopid=c.shopid", " c.investorid=" + Method.CurrentUserID + Where_String, "a.[ID] desc", Pager.CurrentPageIndex,
                Pager.PageSize, PubConstant.DataBaseTarget.JHShop, out Message);

            if (!string.IsNullOrEmpty(Message))
            {
                MessageBox box = new MessageBox();
                box.ErrorBox(this, "数据查询失败!");
                return;
            }
            grdlist.DataSource = Page_Dt;
            grdlist.DataBind();
            this.Pager.RecordCount = RecordCount;
        }

        protected void grdlist_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                int indexID = (this.Pager.CurrentPageIndex - 1) * this.Pager.PageSize + e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = indexID.ToString();
            }
        }

        protected void Pager_BindData(object sender, EventArgs e)
        {
            Bind((hidWhere.Value ?? "").ToString());
        }

        protected void ddlistShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlistShop.SelectedItem.Value == "0")
            {
                hidWhere.Value = "";
            }
            else
            {
                hidWhere.Value = " and a.ShopID=" + ddlistShop.SelectedItem.Value;
            }
            this.Pager.CurrentPageIndex = 1;
            Bind((hidWhere.Value ?? "").ToString());
        }
    }
}